/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示输出调制后的38K红外调制信号
  *          通过GTIM1_CH1 与 ATIM_CH1 “逻辑或” 调制出波形
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：断开J10、J23、J28
  *                    短接J11、J27
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_gtim.h"
#include "sym32l010_hal_atim.h"
#include "sym32l010_hal_irmod.h"
#include "sym32l010_Startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
GTIM_HandleTypeDef hgtim;
ATIM_HandleTypeDef hatim;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void GTIM_Init(void);
static void ATIM_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    // 初始化GTIM
    GTIM_Init();

    // 初始化ATIM
    ATIM_Init();

    // 初始化IR所用到的管脚
    Bsp_IrPort_Init();

    // 配置IR模式
    HAL_IRMOD_Config(IRMOD_GTIM1CH1_OR_ATIMCH1_OR_IRSW, IRMOD_POLARITY_POSITIVE);
    HAL_IRMOD_IRSW_SETLOW();                                          // 设置IRSW为0

    // 请从PA06及PA03观察输出GTIM_CH1及红外调制信号
    while (1)
    {
        while (!HAL_GTIM_GET_FLAG(&hgtim, GTIM_FLAG_UPDATE)) { ; }
        HAL_GTIM_CLEAR_FLAG(&hgtim, GTIM_FLAG_UPDATE);

        uint16_t ccr = HAL_GTIM_GET_AUTORELOAD(&hgtim);
        ccr = (HAL_GTIM_GET_COMPARE(&hgtim, GTIM_CHANNEL_1) + 1) % ccr;
        HAL_GTIM_SET_COMPARE(&hgtim, GTIM_CHANNEL_1, ccr);
    }
}


/*******************************************************************************
 * @brief  GTIM初始化
 * @param  None
 * @retval None
 */
static void GTIM_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    // 外设时钟使能
    HAL_SYSCTRL_GTIM1_CLK_ENABLE();
    HAL_SYSCTRL_GPIOA_CLK_ENABLE();

    // 配置GTIM_CH1管脚
    AFx_GTIM1CH1_PA06();
    GPIO_InitStruct.Pins = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);

    //------------------------------------------------------------
    // 配置GTIM
    MemClr(&hgtim, sizeof(hgtim));

    hgtim.Instance               = SYM_GTIM1;
    hgtim.Init.ClockDivision     = GTIM_CLOCKDIVISION_DIV1;
    hgtim.Init.CounterMode       = GTIM_COUNTERMODE_UP;
    hgtim.Init.Prescaler         = 24 - 1;
    hgtim.Init.Period            = 1263 - 1;                          // 频率 = 48MHz / 24 / 1263 = 1.58K
    hgtim.Init.AutoReloadPreload = GTIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_GTIM_Base_Init(&hgtim);

    hgtim.OcInit.OCMode       = GTIM_OCMODE_PWM1;
    hgtim.OcInit.Pulse        = 0;
    hgtim.OcInit.OCPolarity   = GTIM_OCPOLARITY_HIGH;
    hgtim.OcInit.OCFastMode   = GTIM_OCFAST_DISABLE;
    HAL_GTIM_OC_ConfigChannel(&hgtim, &hgtim.OcInit, GTIM_CHANNEL_1);

    HAL_GTIM_OC_Start(&hgtim, GTIM_CHANNEL_1);
}


/*******************************************************************************
 * @brief  ATIM初始化
 * @param  None
 * @retval None
 */
static void ATIM_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    // 外设时钟使能
    HAL_SYSCTRL_ATIM_CLK_ENABLE();
    HAL_SYSCTRL_GPIOB_CLK_ENABLE();

    // 配置ATIM_CH1管脚
    AFx_ATIMCH1_PB00();
    GPIO_InitStruct.Pins = GPIO_PIN_0;;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOB, &GPIO_InitStruct);

    // 配置ATIM
    MemClr(&hatim, sizeof(hatim));

    hatim.Instance               = SYM_ATIM;
    hatim.Init.ClockDivision     = ATIM_CLOCKDIVISION_DIV1;
    hatim.Init.CounterMode       = ATIM_COUNTERMODE_UP;
    hatim.Init.Prescaler         = 1 - 1;
    hatim.Init.Period            = 1263 - 1;                          // 频率 = 48MHz / 1263 = 38K
    hatim.Init.AutoReloadPreload = ATIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_ATIM_Base_Init(&hatim);

    hatim.OcInit.OCMode       = ATIM_OCMODE_PWM1;
    hatim.OcInit.Pulse        = 379 - 1;                              // 379 / 1263 = 30%
    hatim.OcInit.OCPolarity   = ATIM_OCPOLARITY_HIGH;
    hatim.OcInit.OCFastMode   = ATIM_OCFAST_DISABLE;
    HAL_ATIM_OC_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_1);

    HAL_ATIM_OC_Start(&hatim, ATIM_CHANNEL_1);
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
